package me.jessyan.armscomponent.app.mvp.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.app.R;

/**
 * description:
 * https://github.com/xiaobinAndroid421726260/BrokenLineDemo/blob/master/app/src/main/java/com/dbz/example/MainActivity.java
 *
 * @author Db_z
 * date 2020/3/18 14:49
 * @version V1.0
 */
public class BrokenLineView extends View {
    private String title = "";
    private String month = "";
    private String unit = "";

    //xy坐标轴颜色
    private int mXYLineColor = 0;
    /**
     * xy坐标轴线的宽度
     */
    private int mXYLineWidth = 1;
    /**
     * xy坐标轴文字颜色
     */
    private int mXyTextColor = 0;
    /**
     * xy坐标轴文字大小
     */
    private int mXyTextSize = dpToPx(13);
    /**
     * 折线图中折线的颜色
     */
    private int mLinecolor = 0;
    /**
     * 折线图中点位信息颜色
     */
    private int mContentColor = 0;

    /**
     * x轴各个坐标点水平间距
     */
    private int interval = dpToPx(30);
    /**
     * 背景颜色
     */
    private int bgcolor = 0;
    /**
     * 绘制XY轴坐标对应的画笔
     */
    private Paint xyPaint;
    /**
     * 绘制XY轴的文本对应的画笔
     */
    private Paint xyTextPaint;
    /**
     * 画折线对应的画笔
     */
    private Paint mLinePaint;
    /**
     * 画折线对应的显示数据画笔
     */
    private Paint showPaint;
    private int width;
    private int height;
    /**
     * x轴的原点坐标
     */
    private int xOrigin;
    /**
     * y轴的原点坐标
     */
    private int yOrigin;
    /**
     * 第一个点X的坐标
     */
    private float xInit;
    /**
     * y轴坐标对应的数据
     */
    private List<YValue> yValues = new ArrayList<>();
    /**
     * 折线对应的数据
     */
    private List<LineValue> lineValues = new ArrayList<>();
    /**
     * 当前选中点
     */
    private int selectIndex = 1;
    /**
     * X轴刻度文本对应的最大矩形，为了选中时，在x轴文本画的框框大小一致
     */
    private Rect xValueRect;

    /**
     * X轴文本的高度
     */
    private int xTextHeight;

    public BrokenLineView(Context context) {
        this(context, null);
    }

    public BrokenLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BrokenLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initPaint();
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        xyPaint = new Paint();
        xyPaint.setAntiAlias(true);
        xyPaint.setStrokeWidth(mXYLineWidth);
        xyPaint.setStrokeCap(Paint.Cap.ROUND);
        xyPaint.setColor(mXYLineColor);

        xyTextPaint = new Paint();
        xyTextPaint.setAntiAlias(true);
        xyTextPaint.setTextSize(mXyTextSize);
        xyTextPaint.setColor(mXyTextColor);
        xyTextPaint.setStyle(Paint.Style.FILL);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(mXYLineWidth);
        mLinePaint.setStrokeCap(Paint.Cap.ROUND);
        mLinePaint.setColor(mLinecolor);
        mLinePaint.setStyle(Paint.Style.STROKE);

        showPaint = new Paint();
        showPaint.setAntiAlias(true);
        showPaint.setStrokeWidth(mXYLineWidth);
        showPaint.setStrokeCap(Paint.Cap.ROUND);
        showPaint.setColor(Color.parseColor("#6f6f6f"));
        showPaint.setStyle(Paint.Style.FILL);
        showPaint.setTextSize(mXyTextSize);
    }

    /**
     * 初始化自定义属性
     */
    private void initView(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.brokenLineView);
        mXYLineColor = array.getColor(R.styleable.brokenLineView_dbz_xy_line_color, mXYLineColor);
        mXYLineWidth = (int) array.getDimension(R.styleable.brokenLineView_dbz_xy_line_width, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mXYLineWidth, getResources().getDisplayMetrics()));
        mXyTextColor = array.getColor(R.styleable.brokenLineView_dbz_xy_text_color, mXyTextColor);
        mXyTextSize = (int) array.getDimension(R.styleable.brokenLineView_dbz_xy_text_size, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mXyTextSize, getResources().getDisplayMetrics()));
        mLinecolor = array.getColor(R.styleable.brokenLineView_dbz_line_color, mLinecolor);
        mContentColor = array.getColor(R.styleable.brokenLineView_dbz_content_color, mContentColor);
        bgcolor = array.getColor(R.styleable.brokenLineView_dbz_bg_color, bgcolor);
        interval = (int) array.getDimension(R.styleable.brokenLineView_dbz_interval, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, interval, getResources().getDisplayMetrics()));
        array.recycle();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (changed) {
            //得到宽度高度
            width = getWidth();
            height = getHeight();
            xOrigin = mXYLineWidth;//dp2是y轴文本距离左边，以及距离y轴的距离
            interval = (width - getPaddingLeft() - getPaddingRight()) / 7;
            // 要把底部文本的高度留出来
            yOrigin = height - 120;//dp3是x轴文本距离底边，dp2是x轴文本距离x轴的距离
            xInit = 45;
        }
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(bgcolor);
        drawBrokenLineAndPoint(canvas);
    }

    /**
     * 绘制折线和折线点
     */
    private void drawBrokenLineAndPoint(Canvas canvas) {
        //设置显示折线的图层
        int layer = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);
        drawBrokenLine(canvas);
        drawBrokenPoint(canvas);
        xyTextPaint.setColor(Color.parseColor("#000000"));
        canvas.drawText(title, 10, 30, xyTextPaint);
        canvas.drawText(unit, width - 180, 30, xyTextPaint);
        xyTextPaint.setColor(Color.parseColor("#353535"));
        canvas.drawText(month, 10, height - 5, xyTextPaint);
        //保存图层
        canvas.restoreToCount(layer);
    }

    /**
     * 绘制折线对应的点位圆圈
     */
    private void drawBrokenPoint(Canvas canvas) {
        float dp3 = dpToPx(3);
        float dp5 = dpToPx(5);
        //绘制节点对应的原点
        for (int i = 0; i < lineValues.size(); i++) {
            float x = xInit + interval * i;
            float y = (float) (yOrigin - (1 + 0.3f) * lineValues.get(i).num);
            //绘制折线点
            mLinePaint.setStyle(Paint.Style.FILL);
            mLinePaint.setColor(Color.WHITE);
            canvas.drawCircle(x, y, dp3, mLinePaint);
            if (Double.parseDouble(lineValues.get(i).value) > 0) {
                xyTextPaint.setColor(Color.parseColor("#6CBC59"));
                canvas.drawText(lineValues.get(i).value, x - 20, y - 25, xyTextPaint);
            }
            xyTextPaint.setColor(Color.parseColor("#8E8E8E"));
            canvas.drawText(yValues.get(i).value, x - 20, y + 50, xyTextPaint);
            mLinePaint.setStyle(Paint.Style.STROKE);
            mLinePaint.setColor(mLinecolor);
            canvas.drawCircle(x, y, dp3, mLinePaint);
        }
    }

    /**
     * 绘制折线
     */
    private void drawBrokenLine(Canvas canvas) {
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(mLinecolor);
        //绘制折线
        Path path = new Path();
        float x = xInit + interval * 0;
        float y = yOrigin;
        path.moveTo(x, y);
        for (int i = 1; i < lineValues.size(); i++) {
            x = xInit + interval * i;
            y = (float) (yOrigin - (1 + 0.3f) * lineValues.get(i).num);
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            path.lineTo(x, y);
        }
        canvas.drawPath(path, mLinePaint);
    }

    public void setMainValue(String title, String month, String unit) {
        this.title = title;
        this.month = month;
        this.unit = unit;
    }

    public void setValue(List<LineValue> lineValues, List<YValue> yValues) {
        this.lineValues = lineValues;
        this.yValues = yValues;
        invalidate();
    }

    /**
     * 获取丈量文本的矩形
     *
     * @param text
     * @param paint
     * @return
     */
    private Rect getTextBounds(String text, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect;
    }

    /**
     * dp转化成为px
     *
     * @param dp
     * @return
     */
    private int dpToPx(int dp) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f * (dp >= 0 ? 1 : -1));
    }

    public static class XValue {
        public double num;
        public String value;

        public XValue(double num, String value) {
            this.num = num;
            this.value = value;
        }
    }

    public static class YValue {
        public double num;
        public String value;

        public YValue(double num, String value) {
            this.num = num;
            this.value = value;
        }
    }

    public static class LineValue {
        //具体数值
        public double num;
        //显示值
        public String value;

        public LineValue(double num, String value) {
            this.num = num;
            this.value = value;
        }
    }
}